package com.oinux.playhelper.ui;

import java.sql.SQLException;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.oinux.playhelper.R;
import com.oinux.playhelper.adapter.AppStatusAdapter;
import com.oinux.playhelper.app.BaseActivity;
import com.oinux.playhelper.db.DatabaseHelper;
import com.oinux.playhelper.entity.AppInfo;
import com.umeng.analytics.MobclickAgent;

public class AppStatus extends BaseActivity implements OnClickListener {

	private static final String TAG = "app_status_activity";

	private TextView titleText;
	private ListView installedAppListview;

	private AppStatusAdapter appInstalledAdapter;
	private DatabaseHelper databaseHelper = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_status);
		initView();
		initData();
	}


	private DatabaseHelper getHelper() {
		if (databaseHelper == null) {
			databaseHelper = OpenHelperManager.getHelper(this,
					DatabaseHelper.class);
		}
		return databaseHelper;
	}

	private void initData() {
		List<AppInfo> appInfoList = null;
		try {
			QueryBuilder<AppInfo, Integer> queryBuilder = getHelper()
					.getAppInfoDao().queryBuilder();
			queryBuilder.orderBy("isRunning", false).orderBy("runTime", false)
					.where().eq("isInstall", true);
			PreparedQuery<AppInfo> preparedQuery = queryBuilder.prepare();
			appInfoList = getHelper().getAppInfoDao().query(preparedQuery);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		int runningColor = getResources().getColor(R.color.running_text_wz);
		if (getThemeId() == R.style.qinxin) {
			runningColor = getResources().getColor(R.color.running_text);
		}
		appInstalledAdapter = new AppStatusAdapter(this, appInfoList,
				runningColor);
		installedAppListview.setAdapter(appInstalledAdapter);
	}

	private void initView() {
		titleText = (TextView) findViewById(R.id.title_bar_text);
		titleText.setText(R.string.app_status);
		installedAppListview = (ListView) findViewById(R.id.installed_app_list);
		installedAppListview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(AppStatus.this, AppDetail.class);
				AppInfo appInfo = (AppInfo) parent.getItemAtPosition(position);
				intent.putExtra("app_id", appInfo.getId());
				startActivity(intent);
			}
		});
	}

	@Override
	public void onClick(View v) {
	}

	@Override
	protected void onDestroy() {
		if (databaseHelper != null) {
			OpenHelperManager.releaseHelper();
			databaseHelper = null;
		}
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
